package com.wh.common.vo;

import com.wh.common.util.TraceUtil;
import lombok.Data;

/**
 * @author luweije
 */
@Data
public class Result<T> {

    private static final int SUCCESS_CODE = 0;

    private int code;

    private String message;

    private T data;

    private String traceId = TraceUtil.getTraceIdFromMdc();

    public boolean isOk() {
        return this.code == SUCCESS_CODE;
    }

    public static <T> Result<T> successResponse() {
        Result<T> result = new Result<>();
        result.setCode(SUCCESS_CODE);
        result.setMessage("success");
        return result;
    }

    public static <T> Result<T> successResponse(T data) {
        Result<T> result = new Result<>();
        result.setCode(SUCCESS_CODE);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> failResponse() {
        Result<T> result = new Result<>();
        result.setCode(1);
        result.setMessage("执行失败");
        return result;
    }

    public static <T> Result<T> failResponse(int code) {
        Result<T> result = new Result<>();
        result.setCode(code);
        return result;
    }

    public static <T> Result<T> failResponse(String message) {
        Result<T> result = new Result<>();
        result.setCode(1);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> failResponse(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

}
